package in.waffl.fairycake;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.io.IOException;

public class Spark extends Projectile {

	public Spark(int x, int y, float hspeed, final Canvas canvas) {
		super(x, y, 20, 20);
		try {
			setSprite(new Sprite(Ptolemy.class.getResourceAsStream("/in/waffl/fairycake/data/spark.png"), 19, 19));
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.drawable = new Drawable() {
			@Override
			public void draw(Graphics2D g) {
				getSprite().draw(g, canvas, getX(), getY()-29, getImageIndex());
			}
		};
		drawable.depth = -10;
		canvas.addDrawable(drawable);
		this.setGravity(0);
		this.setHspeed(hspeed);
	}

	protected void projectilePoll(PollingThread p) {
		Rectangle r = new Rectangle(getX(), getY()-29, getWidth(), getHeight());
		Monster nowCake = Monster.collisionRectangle(r);
		if (nowCake != null) {
			nowCake.cakify();
			this.destroy();
		}
		Cake stillCake = Cake.collisionRectangle(r);
		if (stillCake != null) {
			stillCake.setDamage(stillCake.getDamage() + 1);
			this.destroy();
		}
		getSprite().setAlpha(getSprite().getAlpha() - 0.05f);
		if (getSprite().getAlpha() <= 0) {
			this.destroy();
		}
	}

	protected void onLanding() {
	}

}
